csplit命令

csplit命令将用PATTERN分隔的FILE文件输出到文件xx00xx01...,并将每个文件的字节数输出到标准输出。

语法

csplit [OPTION]... FILE PATTERN...

参数

模式

如果文件被指定为破折号-csplit将读取标准输入,每种模式可能是:

示例

list.txt文件内容如下:

1. Apples
2. Bananas
3. Oranges
4. Pineapples
5. Guava

使用csplit命令将此文件分为两部分,第二部分从第三行开始。

csplit list.txt 3
# 21
# 34

cat xx00
# 1. Apples
# 2. Bananas

cat xx01
# 3. Oranges
# 4. Pineapples
# 5. Guava

使用csplit命令将此文件分为三部分。

csplit list.txt 2 3
# 21
# 34

cat xx00
# 1. Apples

cat xx01
# 2. Bananas

cat xx02
# 3. Oranges
# 4. Pineapples
# 5. Guava

使用自定义的分割前缀aa代替xx分割前缀。

csplit list.txt -f aa 3
# 21
# 34

ls
# aa00  aa01 list.txt

使用三位数字代替默认的两位数字。

csplit list.txt -n 3 3
# 21
# 34

ls
# list.txt  xx000  xx001

使用模式定义分割规则,重复上一个模式指定的次数。

csplit list.txt 2 {1}
# 10
# 22
# 23

ls xx*
# xx00  xx01  xx02

参考

https://www.computerhope.com/unix/ucsplit.htm
https://www.runoob.com/linux/linux-comm-csplit.html
https://www.geeksforgeeks.org/csplit-command-in-linux-with-examples/